﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TYBase;
using TYControl;
using TYRTUBase;

namespace TYControl_For_RTU
{
    public partial class View_Config_Device : WindowT
    {
        private ViewModel_Config_Device vm = new ViewModel_Config_Device();

        public View_Config_Device(List<DeviceBase> baseComHelpers, INI ini)
        {
            InitializeComponent();
            DataCenter.ini = ini;
            DataCenter.mDeviceHelper = new DeviceCreator(ini);
            this.DataContext = vm;
            BaseComHelpers = baseComHelpers;
            this.Loaded += View_Config_Com_2_LoadedAsync;
        }

        private ObservableCollection<ViewModelComBase> ListComInfo = new ObservableCollection<ViewModelComBase>();

        private ObservableCollection<ViewModelComBase> GetComList()
        {
            var comInfo = SystemHelper.GetComInfo();
            ObservableCollection<ViewModelComBase> ListComModel = new ObservableCollection<ViewModelComBase>();
            foreach (var item in comInfo)
            {
                ListComModel.Add(new ViewModelComBase
                {
                    Com = item.Key,
                    ComInfo = $"COM{item.Key}-{item.Value}",
                });
            }
            return ListComModel;
        }

        private async void View_Config_Com_2_LoadedAsync(object sender, RoutedEventArgs e)
        {
            foreach (var item in BaseComHelpers)
            {
                vm.ListEquipInfo.Add(new EquipInfoModel(item.EquipType) { EquipId = item.EquipId, Name = item.EquipName, EquipType = item.EquipType });
            }
            ListComInfo = await Task.Run(() =>
              {
                  return GetComList();
              });
            if (vm.ListEquipInfo.Count > 0)
                vm.SelectedEquipInfo = vm.ListEquipInfo[0];
        }

        public List<DeviceBase> BaseComHelpers { get; }

        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (vm.SelectedEquipInfo == null)
            {
                MessageBox.Show("选择为空!");
                return;
            }

            var comHelper = BaseComHelpers.FirstOrDefault(x => x.EquipId == vm.SelectedEquipInfo.EquipId);
            if (comHelper == null)
            {
                return;
            }
            switch (comHelper.EquipType)
            {
                case EnumEquipType.IR:
                    ctrl.Content = new View_Config_Device_IR(comHelper as Device_IR, ListComInfo);

                    break;

                case EnumEquipType.Switch:
                    ctrl.Content = new View_Config_Device_Switch(comHelper as Device_Switch, ListComInfo);
                    break;

                case EnumEquipType.Scaner:
                    ctrl.Content = new View_Config_Device_Scaner(comHelper as Device_Scaner, ListComInfo);
                    break;

                default:
                    break;
            }
        }
    }
}